package com.kensure.shike.cms.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.MapUtils;
import co.kensure.mem.PageInfo;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.cms.dao.SKCmsDao;
import com.kensure.shike.cms.model.SKCms;
import com.kensure.shike.cms.model.SKCmsText;

/**
 * 消息内容服务实现类
 * 
 * @author fankd created on 2019-9-22
 * @since
 */
@Service
public class SKCmsService extends JSBaseService {

	@Resource
	private SKCmsDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKCmsTextService sKCmsTextService;

	public SKCms selectOne(Long id) {
		return dao.selectOne(id);
	}
	
	public SKCms detail(Long id) {
		SKCms cms = selectOne(id);
		SKCmsText detail = sKCmsTextService.selectOne(id);
		if(detail != null){
			cms.setContent(detail.getContent());
		}else{
			cms.setContent("");
		}
		
		return cms;
	}

	public List<SKCms> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKCms> selectAll() {
		return dao.selectAll();
	}

	public List<SKCms> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public List<SKCms> selectByWhere(PageInfo page) {
		Map<String, Object> parameters = MapUtils.genMap("orderby", "typeid,disorder");
		MapUtils.putPageInfo(parameters, page);
		return selectByWhere(parameters);
	}

	public long selectCount() {
		return dao.selectCount();
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKCms obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_cms"));
		return dao.insert(obj);
	}

	public boolean update(SKCms obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	public List<SKCms> selectByTypeId(Integer typeid) {
		Map<String, Object> parameters = MapUtils.genMap("typeid", typeid, "orderby", "disorder");
		return selectByWhere(parameters);
	}

	public void add(SKCms obj) {
		ParamUtils.isBlankThrewException(obj.getTitle(), "标题不能为空");
		if (obj.getId() != null) {
			update(obj);
			SKCmsText t = new SKCmsText();
			t.setId(obj.getId());
			t.setContent(obj.getContent());
			sKCmsTextService.update(t);
		} else {
			insert(obj);
			SKCmsText t = new SKCmsText();
			t.setId(obj.getId());
			t.setContent(obj.getContent());
			sKCmsTextService.insert(t);
		}
	}

}
